/**
 * @title BufferManage.java
 * @package com.esun.cheetah.biz.match.matchlist
 * @description 获得远程数据以及管理缓存
 * @author liugan
 * @date 2012-4-18
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.match.matchlist;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.esun.cheetah.model.match.matchlist.Match;
import com.esun.cheetah.model.match.matchlist.Match.ShowState;

/**
 * @title BufferManage
 * @description 管理比赛列表相关数据,该类中的所有方法以及数据都只能在UI线程使用，以保证该类的线程安全性
 * @author liugan
 */
public class MatchListManager {
	private static Format format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	private static MatchListManager instance = new MatchListManager();

	private int playingCount,unplayedCount,overCount,attentionedCount,recommendCount,attentionedPlayingCount;
	//用于标识各个列表的数据是否过时
	private boolean mRecommendNeedUpdate,mAllNeedUpdate,mPlayingNeedUpdate,mUnplayedNeedUpdate,mOverNeedUpdate,mAttentionedNeedUpdate;
	
	
	private ArrayList<Match> mTotalMatches = new ArrayList<Match>();
	private ArrayList<Match> mAllMatches = new ArrayList<Match>();
	private ArrayList<Match> mRecommendMatches = new ArrayList<Match>();
	private ArrayList<Match> mPlayingMatches = new ArrayList<Match>();
	private ArrayList<Match> mUnplayedMatches = new ArrayList<Match>();
	private ArrayList<Match> mOverMatches = new ArrayList<Match>();
	private ArrayList<Match> mAttentionedMatches = new ArrayList<Match>();
	
	private String mUpdateTime = "--";
	/**
	 * @title updateMatches
	 * @description 更新比赛列表数据
	 * @param matches
	 */
	public void updateMatches(ArrayList<Match> matches){
		mTotalMatches.clear();
		mTotalMatches.addAll(matches);
		updateMatchLists(mTotalMatches);
		
		Date now = new Date();
		mUpdateTime = format.format(now);
	}
	
	
	public String getUpdateTime(){
		return mUpdateTime;
	}
	/**
	 * @title updateMatchLists
	 * @description 分配各个比赛列表数据
	 * 	@param matches 
	 */
	private void updateMatchLists(ArrayList<Match> matches) {
		mAllMatches.clear();
		mRecommendMatches.clear();
		mPlayingMatches.clear();
		mUnplayedMatches.clear();
		mOverMatches.clear();
		mAttentionedMatches.clear();
		
	    int attentionedPlaying = 0;
		
		for(Match match : matches){
			
			switch(match.getShowState()){
			case playing:
				mAllMatches.add(match);
				mRecommendMatches.add(match);
				mPlayingMatches.add(match);
				if(match.isMarked()){
					mAttentionedMatches.add(match);
					attentionedPlaying ++;
				}
				break;
			case unplayed:
				mAllMatches.add(match);
				mRecommendMatches.add(match);
				mUnplayedMatches.add(match);
				if(match.isMarked()){
					mAttentionedMatches.add(match);
				}
				break;
			case over:
				mAllMatches.add(match);
				mRecommendMatches.add(match);
				mOverMatches.add(match);
				if(match.isMarked()){
					mAttentionedMatches.add(match);
				}
				break;
			default:
				break;
			}
		}
		
		
		attentionedPlayingCount = attentionedPlaying;
		recommendCount = mRecommendMatches.size();
		playingCount = mPlayingMatches.size();
		unplayedCount = mUnplayedMatches.size();
		overCount = mOverMatches.size();
		attentionedCount = mAttentionedMatches.size();
		
		mAllNeedUpdate = true;
		mRecommendNeedUpdate = true;
		mPlayingNeedUpdate = true;
		mUnplayedNeedUpdate = true;
		mOverNeedUpdate = true;
		mAttentionedNeedUpdate = true;
	}

	private MatchListManager() {
	}

	public static MatchListManager getInstance() {
		return instance;
	}

	public int getPlayingCount() {
		return playingCount;
	}

	public void setPlayingCount(int playingCount) {
		this.playingCount = playingCount;
	}

	public int getUnplayedCount() {
		return unplayedCount;
	}

	public void setUnplayedCount(int unplayedCount) {
		this.unplayedCount = unplayedCount;
	}

	public int getOverCount() {
		return overCount;
	}

	public void setOverCount(int overCount) {
		this.overCount = overCount;
	}

	public int getAttentionedCount() {
		return attentionedCount;
	}

	public void setAttentionedCount(int attentionedCount) {
		this.attentionedCount = attentionedCount;
	}

	public int getRecommendCount() {
		return recommendCount;
	}

	public void setRecommendCount(int recommendCount) {
		this.recommendCount = recommendCount;
	}

	public boolean isRecommendNeedUpdate() {
		return mRecommendNeedUpdate;
	}

	public void setRecommendNeedUpdate(boolean mRecommendNeedUpdate) {
		this.mRecommendNeedUpdate = mRecommendNeedUpdate;
	}

	public boolean isAllNeedUpdate() {
		return mAllNeedUpdate;
	}

	public void setAllNeedUpdate(boolean mAllNeedUpdate) {
		this.mAllNeedUpdate = mAllNeedUpdate;
	}

	public boolean isPlayingNeedUpdate() {
		return mPlayingNeedUpdate;
	}

	public void setPlayingNeedUpdate(boolean mPlayingNeedUpdate) {
		this.mPlayingNeedUpdate = mPlayingNeedUpdate;
	}

	public boolean isUnplayedNeedUpdate() {
		return mUnplayedNeedUpdate;
	}

	public void setUnplayedNeedUpdate(boolean mUnplayedNeedUpdate) {
		this.mUnplayedNeedUpdate = mUnplayedNeedUpdate;
	}

	public boolean isOverNeedUpdate() {
		return mOverNeedUpdate;
	}

	public void setOverNeedUpdate(boolean mOverNeedUpdate) {
		this.mOverNeedUpdate = mOverNeedUpdate;
	}

	public boolean isAttentionedNeedUpdate() {
		return mAttentionedNeedUpdate;
	}

	public void setAttentionedNeedUpdate(boolean mAttentionedNeedUpdate) {
		this.mAttentionedNeedUpdate = mAttentionedNeedUpdate;
	}

	public ArrayList<Match> getTotalMatches() {
		return mTotalMatches;
	}

	public void setTotalMatches(ArrayList<Match> mTotalMatches) {
		this.mTotalMatches = mTotalMatches;
	}

	public ArrayList<Match> getAllMatches() {
		return mAllMatches;
	}

	public void setAllMatches(ArrayList<Match> mAllMatches) {
		this.mAllMatches = mAllMatches;
	}

	public ArrayList<Match> getRecommendMatches() {
		return mRecommendMatches;
	}

	public void setRecommendMatches(ArrayList<Match> mRecommendMatches) {
		this.mRecommendMatches = mRecommendMatches;
	}

	public ArrayList<Match> getPlayingMatches() {
		return mPlayingMatches;
	}

	public void setPlayingMatches(ArrayList<Match> mPlayingMatches) {
		this.mPlayingMatches = mPlayingMatches;
	}

	public ArrayList<Match> getUnplayedMatches() {
		return mUnplayedMatches;
	}

	public void setUnplayedMatches(ArrayList<Match> mUnplayedMatches) {
		this.mUnplayedMatches = mUnplayedMatches;
	}

	public ArrayList<Match> getOverMatches() {
		return mOverMatches;
	}

	public void setOverMatches(ArrayList<Match> mOverMatches) {
		this.mOverMatches = mOverMatches;
	}


	public ArrayList<Match> getAttentionedMatches() {
		return mAttentionedMatches;
	}


	public void setAttentionedMatches(ArrayList<Match> mAttentionedMatches) {
		this.mAttentionedMatches = mAttentionedMatches;
	}


	public int getAttentionedPlayingCount() {
		return attentionedPlayingCount;
	}


	public void setAttentionedPlayingCount(int attentionedPlayingCount) {
		this.attentionedPlayingCount = attentionedPlayingCount;
	}


	/**
	 * @title attentioned
	 * @description 关注一场新的比赛时,更新比赛数据
	 * @param match
	 */
	public void attentioned(Match match) {
		if(match == null){
			return;
		}
		
		if(!mAttentionedMatches.contains(match)){
			mAttentionedMatches.add(match);
		}
		attentionedCount = mAttentionedMatches.size();
		if(match.getShowState() == ShowState.playing){
			attentionedPlayingCount ++;
		}
		
	}


	/**
	 * @title cancelAttentioned
	 * @description 取消关注一场比赛时，更新数据
	 * @param match
	 */
	public void cancelAttentioned(Match match) {
		if(match == null){
			return;
		}
		
		if(mAttentionedMatches.contains(match)){
			mAttentionedMatches.remove(match);
		}
		attentionedCount = mAttentionedMatches.size();
		if(match.getShowState() == ShowState.playing){
			attentionedPlayingCount --;
		}
	}

	
}
